<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" maxlength="10" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="form.price" maxlength="6" placeholder="价格"
                  oninput="value=value.replace(/^\D*([0-9]\d*\.?\d{0,2})?.*$/,'$1')"
                  @blur="form.price=$event.target.value"></el-input>
      </el-form-item>
      <el-form-item label="份数" prop="number">
        <el-input v-model="form.number" maxlength="10" placeholder="份数"
                  oninput="value=value.replace(/^\D*([0-9]+)?.*$/,'$1')"
                  @blur="form.number=$event.target.value"></el-input>
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input v-model="form.unit" maxlength="10" placeholder="单位"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" placeholder="请输入排序"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary"
                   @click="handlerSubmit" v-has-permi="['admin:merchantActivities:buying:saveSon']">保存
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {saveClassificationSon} from "@/api/merchantActivity";

export default {
  props: {
    tableData: {
      type: Object,
      default: () => {
        return {}
      }
    },
    id: {
      default: null
    },
    typeId: {
      default: null
    },
  },
  data() {
    return {
      form: {
        categoryId: null,
        name: '',
        price: 0,
        number: 0,
        unit: '',
        sort: 0,
      },
      rules: {
        name: [{required: true, message: '请填写名称', trigger: ['blur']}],
        price: [{required: true, message: '请填写价格', trigger: ['blur']}],
        unit: [{required: true, message: '请填写单位', trigger: ['blur']}],
        number: [{required: true, message: '请填写份数', trigger: ['blur']}],
      },
      tree: [],
      saveLoading: false,
      formLoading: false,
      treeLoading: false,
    }
  },
  mounted() {
    this.getDetail();
  },
  methods: {
    getDetail() {
      if (this.tableData) {
        this.form = this.tableData
      }
    },
    handlerSubmit() {
      if (this.saveLoading || this.formLoading || this.treeLoading) {
        return;
      }
      this.$refs.form.validate(valid => {
        if (!valid) return;
        this.saveLoading = true;
        this.form.categoryId = this.typeId;
        saveClassificationSon(this.form).then(() => {
          this.$message.success('保存成功');
          this.close();
        }).finally(() => {
          this.saveLoading = false
        })
      })
    },
    close() {
      this.$emit('hideClassificationSonDialog')
    }
  }
}
</script>
